class Shop::SpusController < ShopController
  def index
    @spus = current_shoper.shop.spus
    if params[:state].presence
      if params[:state] == 'onsale'
        if params[:published].presence
          @spus = @spus.where(published: ActiveModel::Type::Boolean.new.cast(params[:published]))
        end
      elsif params[:state] == 'ontrash'
        @spus = @spus.where(state: :ontrash)
      end
    end
    if params[:check_stock].presence
      if params[:check_stock] == 'normal'
        @spus = @spus.where("stock > min_stock AND stock < max_stock")
      elsif params[:check_stock] == 'zero'
        @spus = @spus.where(stock: 0)
      elsif params[:check_stock] == 'below'
        @spus = @spus.where("stock <= min_stock")
      elsif params[:check_stock] == 'hyper'
        @spus = @spus.where("stock >= min_stock")
      end
    end
    if params[:shop_category_id].presence
      @spus = @spus.where(shop_category_id: params[:shop_category_id])
    end
    if params[:search_key].presence
      @spus = @spus.where("name LIKE ?", "%#{params[:search_key]}%")
    end
    @spus = @spus.order("created_at DESC").page(params[:page]).per(params[:per])
  end
  
  def show
    @spu = Spu.find params[:id]
  end

  def new
    if params[:shop_category_id].presence
      @shop_category = ShopCategory.find params[:shop_category_id]
    end
    @spu = Spu.new
  end

  def create
    @spu = Spu.new spu_params
    @spu.shop_id = current_shoper.shop_id
    @spu.state = :pending
    if @spu.save
      redirect_to shop_spu_path(@spu), notice: "保存成功！"
    else
      render :new
    end
  end

  def edit
    @spu = Spu.find params[:id]
  end

  def update
    @spu = Spu.find params[:id]
    if @spu.update spu_params
      @spu.update(state: :pending)
      redirect_to shop_spus_path, notice: "更新成功！"
    else
      render :edit
    end
  end

  def check
    @spu = Spu.find params[:id]
    if @spu.pending? || @spu.disagree?
      if @spu.update!(state: :oncheck)
        redirect_to shop_spus_path, notice: "提交审核成功！"
      else
        redirect_to shop_spus_path, notice: "提交审核失败！"
      end
    else
      redirect_to shop_spus_path, notice: "提交审核失败，商品状态出错！"
    end
  end
  
  def trash
    @spu = Spu.find params[:id]
    if @spu.update(state: :ontrash)
      redirect_to shop_spus_path, notice: "移入回收站成功！"
    else
      redirect_to shop_spus_path, notice: "移入回收站出错！"
    end
  end

  def published
    @spu = Spu.find params[:id]
    if @spu.update(published: !@spu.published)
      redirect_to shop_spus_path, notice: "#{ @spu.published ? "上架" : "下架" }成功！"
    else
      redirect_to shop_spus_path, notice: "#{ @spu.published ? "上架" : "下架" }出错！"
    end
  end
  
  def destroy
    @spu = Spu.find params[:id]
    if @spu.ontrash?
      if @spu.destroy
        redirect_to shop_spus_path, notice: "删除成功."
      else
        redirect_to shop_spus_path, notice: "删除出错."
      end
    else
      redirect_to shop_spus_path, notice: "删除失败，商品状态出错！"
    end
  end

  def orders
    @spu = Spu.find params[:id]
    @order_items = OrderItem.joins(:order).where("orders.shop_id = ? AND spu_id = ?", current_shoper.shop_id, @spu.id).order("created_at DESC").page(params[:page]).per(params[:per])
  end

  private

  def spu_params
    params.require(:spu).permit(:name, :brand_id, :can_use_coupon, :fare_template_id, :min_stock, :max_stock, :shop_category_id, :spu_category_id, :shop_id, :description, :seven_day_back, :title, :published, :avatar, :genuine_product, :promotion, service_item_ids: [])
  end
end
